/**
 * Created by Julia on 2016/4/28.
 */

define(['jquery', 'appLocalStorage', 'appMessageBox', 'appConst'], function ($, appLocalStorage, msgBox, appConst) {
    var projectKey = 'ispc.account.project.';
    
    return {
        getUserId: function () {
            return $('.d-header-user-id').val();
        },
        getCurrentUserName: function () {
           return $('.d-header-user-name').val();
        },
        getCurrentUserRoles: function () {
            return $('.d-header-user-role').val();
        },
        getPageSize: function () {
            var n = $('.d-header-user-pageSize').val();
            if(n != null && n != "") {
                n = Number(n);
            }
            else {
                n = 10;
            }
            return n;
        },
        getCurrentProject: function () {
            return $('#t_project').attr('data-current-project');
        },
        getProjectLocal: function (uid) {
            return appLocalStorage.getLocal(projectKey + uid);
        },
        setProjectLocal: function (uid, p) {
            appLocalStorage.setLocal(projectKey + uid, p);
        },
        clearProjectLocal: function (uid) {
            appLocalStorage.clearLocal(projectKey + uid);
        },
        hasProjectLocal: function (uid) {
            if (this.getProjectLocal(uid) != null) {
                return true;
            }
            return false;
        },
        hasRole: function (role) {
            var r = this.getCurrentUserRoles().split(',');
            for (var i = 0; i < r.length; i++) {
                if (role == r[i]) {
                    return true;
                }
            }
            return false;
        },
        isAdmin: function () {
            var r = this.getCurrentUserRoles().split(',');
            for (var i = 0; i < r.length; i++) {
                if ('Administrator' == r[i]) {
                    return true;
                }
            }
            return false;
        },
        drawNumber_bg: function (table, callback) {
            table.DataTable().on('draw.dt', function () {
                var dt = $(this).dataTable();
                dt.api().column(0, {draw: 'applied'}).nodes().each(function (cell, i) {
                    var pageNum = dt.api().page();
                    var pageSize = dt.api().page.len();
                    cell.innerHTML = pageNum * pageSize + i + 1;
                });
                if ($.isFunction(callback)) {
                    callback();
                }
            });
        },
        drawNumber_ui: function (table, callback) {
            table.DataTable().on('draw.dt', function () {
                var dt = $(this).dataTable();
                dt.api().column(0, {draw: 'applied'}).nodes().each(function (cell, i) {
                    cell.innerHTML = i + 1;
                });
                if ($.isFunction(callback)) {
                    callback();
                }
            });
        },
        ajaxNoParams: function (url, callback, errCallBack) {
            $.ajax({
                url: url,
                type: 'POST',
                success: function (d) {
                    if (d.code == 0) {
                        if (d.message != null && d.message != "" && d.message.indexOf("mongodb") > 0) {
                            msgBox.errorBox("EXCEPTION_GLOBAL_MONGODB");
                        } else {
                            msgBox.errorBox(d.message);
                        }
                        if ($.isFunction(errCallBack)) {
                            errCallBack(d);
                        }
                    } else {
                        if ($.isFunction(callback)) {
                            callback(d);
                        }
                    }
                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if ($.isFunction(errCallBack)) {
                        errCallBack(e);
                    }
                }
            });
        },
        ajaxHasParams: function (url, params, callback, errCallBack) {
            $.ajax({
                url:url,
                type:"POST",
                contentType: "application/json",
                data:JSON.stringify(params),
                dataType:"json",
                success:function (d){
                    if (d.code == 0) {
                        if (d.message != null && d.message != "" && d.message.indexOf("mongodb") > 0) {
                            msgBox.errorBox("EXCEPTION_GLOBAL_MONGODB");
                        } else {
                            msgBox.errorBox(d.message);
                        }
                        if ($.isFunction(errCallBack)) {
                            errCallBack(d);
                        }
                    } else {
                        if ($.isFunction(callback)) {
                            callback(d);
                        }
                    }
                },
                error:function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if ($.isFunction(errCallBack)) {
                        errCallBack(e);
                    }
                }
            });
        }
    }
});